// pages/testData/index.js
Page({
  data: {
    loading: false
  },

  // 插入测试数据
  async insertTestData() {
    this.setData({ loading: true });
    
    try {
      const result = await wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data: {
          type: 'insertTestData'
        }
      });

      if (result.result.success) {
        wx.showToast({
          title: '测试数据插入成功',
          icon: 'success'
        });
        console.log('插入的数据:', result.result.data);
      } else {
        wx.showToast({
          title: result.result.errMsg || '插入失败',
          icon: 'none'
        });
      }
    } catch (error) {
      console.error('插入测试数据失败:', error);
      wx.showToast({
        title: '插入失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  // 查看数据
  async viewData() {
    try {
      const db = wx.cloud.database();
      const result = await db.collection('games').get();
      
      if (result.data.length > 0) {
        console.log('当前数据:', result.data[0]);
        wx.showToast({
          title: '数据已打印到控制台',
          icon: 'success'
        });
      } else {
        wx.showToast({
          title: '暂无数据',
          icon: 'none'
        });
      }
    } catch (error) {
      console.error('查看数据失败:', error);
      wx.showToast({
        title: '查看失败',
        icon: 'none'
      });
    }
  },

  // 清空games数据库
  async clearGamesDatabase() {
    wx.showModal({
      title: '确认清空',
      content: '确定要清空games数据库吗？此操作不可恢复！',
      confirmText: '确定清空',
      cancelText: '取消',
      confirmColor: '#ff6b6b',
      success: async (res) => {
        if (res.confirm) {
          try {
            console.log('开始清空games数据库');
            
            const result = await wx.cloud.callFunction({
              name: 'quickstartFunctions',
              data: {
                type: 'clearGamesDatabase'
              }
            });
            
            console.log('清空games数据库结果:', result);
            
            if (result.result && result.result.success) {
              wx.showToast({
                title: '清空成功',
                icon: 'success'
              });
            } else {
              wx.showToast({
                title: result.result?.errMsg || '清空失败',
                icon: 'error'
              });
            }
          } catch (error) {
            console.error('清空games数据库失败:', error);
            wx.showToast({
              title: '网络错误',
              icon: 'error'
            });
          }
        }
      }
    });
  },





  // 测试过期检查
  async testExpiredCoupons() {
    try {
      console.log('开始测试过期检查');
      
      const result = await wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data: {
          type: 'testExpiredCoupons'
        }
      });
      
      console.log('测试过期检查结果:', result);
      
      if (result.result && result.result.success) {
        const { data } = result.result;
        const { result: checkResult } = data;
        
        wx.showModal({
          title: '过期检查完成',
          content: `${checkResult.data.message}\n\n检查时间：${new Date(checkResult.data.checkTime).toLocaleString()}\n过期数量：${checkResult.data.expiredCount}`,
          showCancel: false
        });
      } else {
        wx.showToast({
          title: result.result?.errMsg || '检查失败',
          icon: 'error'
        });
      }
    } catch (error) {
      console.error('测试过期检查失败:', error);
      wx.showToast({
        title: '网络错误',
        icon: 'error'
      });
    }
  }
});
